<?php
/**
 * Created by 784855684@qq.com.
 * Link: https://github.com/lichtung/wshore
 * User: linzh
 * Date: 6/30/17
 * Time: 3:15 PM
 */
declare(strict_types=1);


namespace application\module\admin\addon\log\model;


use application\module\admin\addon\model\AddonModel;

/**
 * Class LogModel
 *
 * @property string $action     操作名称
 * @property string $content    操作内容
 * @property string $time       操作时间,自动填充
 * @property int $uid           操作用户ID,未设置时自动从从会话中读取
 *
 * @package application\module\admin\addon\log\model
 */
class LogModel extends AddonModel
{
    public function __construct($uid = null)
    {
        parent::__construct();
        $this->uid = intval($uid);
    }

    protected function struct(): string
    {
        return "  
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `action` varchar(255) NOT NULL COMMENT '操作名称',
  `content` varchar(20480) NOT NULL COMMENT '操作内容',
  `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '操作时间',
  `uid` int(10) NOT NULL COMMENT '操作者ID',
  PRIMARY KEY (`id`)
  ";
    }


    protected function tableName()
    {
        return 'log';
    }

    /**
     * 获取日志列表
     * @param string $date 指定记录日期
     * @param string $keyword 指定关键字
     * @return array
     */
    public function getlist(string $date = '', string $keyword = ''): array
    {
        $where = " uid = {$this->uid} ";
        trim($keyword) and $where .= ' and content like \'%' . addslashes($keyword) . '%\'';
        trim($date) and $where .= ' and date(time) = \'' . $date . '\'';
        $where .= ' order by time desc ;';
        $list = $this->where($where)->select();
        return $list;
    }
}